#define  _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <fstream>
#include "base.h"
#include "child.h"
#include "reg.h"
#include "string.h"

using namespace std;

int nBank;
int hashCount;

char* getResultString(int result);

int main(int argc, char *argv[]){	
	char buffer[100];
	char sriscFileName[255];
	Base *lastMemInst = NULL, *tmpBase;
	char* configFileName = "config.dat";

	//commandline input -- Tested OK!
	strcpy(sriscFileName, argv[1]);
	//printf("%s, %s\n", configFileName, sriscFileName);

	//config file
	ifstream inputFile;
	inputFile.open(configFileName);

	//Memory Banks
	inputFile.getline(buffer, 50);
	inputFile >> nBank;
	inputFile.close();
	
	//Output File
	ofstream resultFile;
	resultFile.open("result.disambiguator");

	//other inits
	hashCount = 0;
	RegFile::init();
	Mem::init();

	//reading .srisc2 file
	ifstream sriscFile;
	sriscFile.open(sriscFileName);
	
	while (sriscFile.eof() != true)
	{
		sriscFile.getline(buffer, 100);
		char* tmppos = strchr(buffer, ';');
		if (tmppos != NULL)
			*tmppos = '\0';
		if (strlen(buffer) == 0) 
			continue;
		tmpBase = Base::initInstruction(buffer);
		if (tmpBase == NULL)
			continue;
		if (tmpBase->isMem())
			if (lastMemInst == NULL)
				lastMemInst = tmpBase;
			else {
				resultFile << tmpBase->getIndex() << ':'
					<< lastMemInst->getIndex() << '=' 
					<< getResultString(tmpBase->checkConflict(lastMemInst))
					<< endl;
				lastMemInst = tmpBase;
			}
	}

	sriscFile.close();
	resultFile.close();
	return 0;
}

char* getResultString(int result){
	switch (result) {
	case YES:
		return "YES";
	case NO:
		return "NO";
	case MAYBE:
		return "MAYBE";
	default:
		return "ERROR";
	}
	return "ERROR";
}
